package com.example.yls.demob;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;

public class MyAdapter extends BaseAdapter {
     private ArrayList<Student> studentArrayList=new ArrayList<>();
     private Context context;
     private delStudent delStudent;

    public MyAdapter(ArrayList<Student> studentArrayList, Context context,delStudent delStudent) {
        this.studentArrayList = studentArrayList;
        this.context = context;
        this.delStudent=delStudent;
    }

    @Override
    public int getCount() {
        return studentArrayList.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

         if (view==null) {
             view = LayoutInflater.from(context).inflate(R.layout.listview_stu, null, false);
             StuListHolder stuListHolder=new StuListHolder();
             stuListHolder.holderAge=view.findViewById(R.id.list_age);
             stuListHolder.holderName=view.findViewById(R.id.list_name);
             stuListHolder.holderStuno=view.findViewById(R.id.list_stuno);
             stuListHolder.delstu=view.findViewById(R.id.del_stu);
             stuListHolder.holderSex=view.findViewById(R.id.list_sex);
             view.setTag(stuListHolder);
         }
         final Student student=studentArrayList.get(i);
         StuListHolder stuListHolder= (StuListHolder) view.getTag();
         stuListHolder.holderStuno.setText(student.getStuno());
         stuListHolder.holderName.setText(student.getName());
         stuListHolder.holderAge.setText(String.valueOf(student.getAge()));
         stuListHolder.holderSex.setText(student.getSex());
         stuListHolder.delstu.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                 delStudent.del_student(student);
             }
         });

        return view;
    }
}
